-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add specific type to Mockito argument matcher any() #27
add specific type to Mockito argument matcher any() #27
Conversation
# Conflicts: # src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java
add specific type to Mockito argument matcher any()
add specific type to Mockito argument matcher any()
@@ -240,6 +241,10 @@ private String deductMatcherTypeMethod(Param param, Language language) { | |||
if (language != Language.Scala) { | |||
matcherType += "()"; | |||
} | |||
// add specific type to any() | |||
if( matcherType.equals("any()")){ | |||
matcherType = addSpecificType(param.getType().getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
need to use
param.getType().getCanonicalName()
otherwise it may not compile.
For instance in updated UT file -\testData\testMeGenerator\mockReturned\test\com\example\services\impl\FooTest.java
:
when(fooFighter.surrender(any(Fear.class), any(Ice.class),
... Fear & Ice types from package com.example.foes - are not imported -
won't work for array types. now that I think of it... there's an existing issue with array types when default mappings exists in TYPE_TO_ARG_MATCHERS ....
-
avoid mutations - it's a bad practice.
please use following implementation. it should resolve issues mentioned above:
@NotNull
private String deductMatcherTypeMethod(Param param, Language language) {
Type type = param.getType();
String matcherMethod = resolveMatcherMethod(type);
if (language == Language.Scala) {
return matcherMethod;
}
else if (!type.isPrimitive() && "any".equals(matcherMethod)) {
return matcherMethod + "("+ type.getCanonicalName()+(type.isArray()? "[]":"") +".class)";
} else {
return matcherMethod+"()";
}
}
private static String resolveMatcherMethod(Type type) {
if (type.isVarargs()) {
return "anyVararg";
} else if(!type.isArray() && TYPE_TO_ARG_MATCHERS.containsKey(type.getCanonicalName())){
return TYPE_TO_ARG_MATCHERS.get(type.getCanonicalName());
} else {
return "any";
}
}
BTW, better replace existing signature of deductMatcherTypeMethod
with resolveMatcherTypeMethod(Type type, Language language)
- caller should pass param.getType() instead of param
Thanks for contributing! happy to see this logic improving
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the solution you proposed and am currently attempting to make the modifications,Thanks
@zhangfj88 - please let me know how you wish to progress this PR. There's a pending comment. Do you plan to handle it? If you don't have time to do so - let me know, I might handle it later. Thanks |
I agree with the solution you proposed and am currently attempting to make the modifications,Thanks |
add specific type to Mockito argument matcher any(),and work for array types
add specific type to Mockito argument matcher any(),and work for arr…
} | ||
|
||
|
||
String addSpecificType(String typeName) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eventually left unused, method can be removed, along with the import statement introducing dependency on apache commons
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks,I will handle it.
} | ||
|
||
@SuppressWarnings("unused") | ||
@Deprecated | ||
public boolean shouldStub(Method testMethod, List<Field> testedClassFields) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please keep this method for now. Although its not used. some users might still use it in their custom templates. so it's only marked as @deprecated for now
# Conflicts: # testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java # testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java # testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java
add specific type to Mockito argument matcher any-- remove unused method
Released in TestMe plugin version 6.4 |
add specific type to Mockito argument matcher any()